package by.kniazhych.core.server.local.rules {
	
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	
	import by.kniazhych.core.constants.PeaceType;
	import by.kniazhych.core.constants.UserColor;
	import by.kniazhych.core.data.game.PeacePosition;
	import by.kniazhych.core.server.local.ai.AI;
	import by.kniazhych.core.server.local.core.IMotion;
	import by.kniazhych.core.server.local.events.RequestMotionEvent;
	import by.kniazhych.core.server.local.events.ServerGameEvent;
	import by.kniazhych.core.server.local.logic.LogicController;
	import by.kniazhych.core.server.local.data.BoardData;
	import by.vaukalak.core.controllers.BaseController;
	import by.vaukalak.core.data.DataBase;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	

	
	public class GameRules extends BaseController{
		
		private var _playerOne:IPlayer;
		private var _playerTwo:IPlayer;
		private var _controller:LogicController;
		
		private var _waitingForPeaceSelection:Boolean = false;
		private var _data:BoardData;
		
		public function GameRules(data:BoardData, player:IPlayer) {
			_data = data;
			_controller = new LogicController(data);
			super(new Sprite(), _controller.data);
			
			var ai:AI = new AI(data, (player.color == UserColor.WHITE ? UserColor.BLACK : UserColor.WHITE))
			_playerOne = player.color == UserColor.WHITE ? player : ai;
			_playerTwo = player.color == UserColor.WHITE ? ai : player;
			
			_playerOne.addEventListener(RequestMotionEvent.REQUEST_MOTION, _processMotion);
			_playerTwo.addEventListener(RequestMotionEvent.REQUEST_MOTION, _processMotion);
			
			_playerOne.addEventListener(ServerGameEvent.FORFEIGHT, _handleForfeight);
			_playerTwo.addEventListener(ServerGameEvent.FORFEIGHT, _handleForfeight);
			
			BoardData(super.dataBase).addEventListener(ServerGameEvent.PEACE_CHANGED, _onPeaceChanged);
			BoardData(super.dataBase).opponentPlayer = _playerTwo;
			BoardData(super.dataBase).currentPlayer = _playerOne;
			
		}
		
		
		
		//--------------------------------------------------------------------------
		//
		//  EVENT HANDLERS
		//
		//--------------------------------------------------------------------------
		
		private function _handleForfeight(e:ServerGameEvent):void {
			_data.endTheGame();
		}
		
		private function _processMotion(e:RequestMotionEvent):void {
			var motion:IMotion = e.motion;
			if (BoardData(super.dataBase).currentPlayer === e.target) {
				if (_controller.processMotion(motion.sP, motion.fP)) {
					var data:BoardData = BoardData(super.dataBase);
					if (!data.currentPlayer.canPromovePawn) {
						_processMotionOutcomes();
					}else {
						if (data.currentPlayer.missingPeaces.length) {							
							_waitingForPeaceSelection = true;
							data.currentPlayer.chosePeace();
						}else {
							var vaukalakPeace:uint = (data.currentPlayer.color == UserColor.WHITE ? PeaceType.WHITE_VAUKALAK : PeaceType.BLACK_VAUKALAK);
							data.currentPlayer.selectPeace(vaukalakPeace);
							_processMotionOutcomes();
						}
					}
				}else {
					e.preventDefault();
				}
			} else {
				e.preventDefault();
			}
		}
		
		private function _processMotionOutcomes():void {
			var data:BoardData = BoardData(super.dataBase);
			var nextPlayer:IPlayer = (data.currentPlayer == _playerOne) ? _playerTwo : _playerOne;
			if (nextPlayer.hasKniaz) {
				data.currentPlayer = nextPlayer;
			}else {
				nextPlayer.declareKniaz();
			}
		}
		
		
		private function _onPeaceChanged(e:ServerGameEvent):void {
			trace("peace changed:\t" + _waitingForPeaceSelection);
			if (_waitingForPeaceSelection) {
				_waitingForPeaceSelection = false;
				_processMotionOutcomes();
			}
		}
		
	}

}